package com.ikas.autumn.iec104.module.protocol.handler;

import com.ikas.autumn.iec104.common.config.IotProtocolAddressConfig;
import com.ikas.autumn.iec104.common.util.IpUtil;
import com.ikas.autumn.iec104.common.util.SpringContextHolder;
import com.ikas.autumn.iec104.module.iec.netty.AbstractClientBuilder;
import com.ikas.autumn.iec104.module.iec.nettyconfig.Iec104MasterBuilder;
import com.ikas.autumn.iec104.module.protocol.entity.Connection;
import com.ikas.autumn.iec104.module.protocol.enums.ProtocolEnum;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class IecProtocolHandler implements ProtocolHandler{

    private static List<Iec104MasterBuilder> masterList = new ArrayList<>();

    private static final String myIp = IpUtil.getIp();

    /**
     * 获取标识
     */
    @Override
    public ProtocolEnum key() {
        return ProtocolEnum.IEC;
    }



    @Override
    public void flushConnect() {

    }


    @Override
    public void clear() {
//        RedisUtil.del(PrefixConstants.IOT_CONDITIONS_IEC_PREFIX + myIp);
//        RedisUtil.del(PrefixConstants.IOT_METES_IEC_PREFIX + myIp);
        masterList.clear();
    }

    @Override
    public void stop() {
        masterList.forEach(AbstractClientBuilder::stop);
    }


    /**
     * 系统启动时初始化
     */
    @Override
    public void initConnect() {
        String iotProtocolAddress = SpringContextHolder.getBean(IotProtocolAddressConfig.class).getIotProtocolAddress();

        log.error("iotProtocolAddress：" + iotProtocolAddress);
        String[] split = iotProtocolAddress.split(":");
        //连接赋值

        List<Connection> connectionList = new ArrayList<>();
        connectionList.add(new Connection(ProtocolEnum.IEC, split[0], split[1]));

//        Map<String, Boolean> connectMap = connectSet.stream().collect(Collectors.toMap(m1 ->  m1.getProtocolEnum().getCode() + "#" + m1.getIp() + "#" + m1.getPort() , m2 ->  false));

        //连接状态入缓存
//        RedisUtil.Hash.putAll(PrefixConstants.IOT_CONDITIONS_IEC_PREFIX + myIp,connectMap);

        //连接
        connect(connectionList);
    }


    /**
     * 启动connectSet容器内所有的连接
     */
    public void connect(List<Connection> connectSet) {
        //连接
        connectSet.forEach(m -> {
            Iec104MasterBuilder i = new Iec104MasterBuilder(
                    m.getIp(), Integer.valueOf(m.getPort()),this
            );
            i.createByUnBlock();
            masterList.add(i);
        });
    }


    /**
     * 连接后，连接成功会被监听，然后调这个方法
     * @param connection
     */
    @Override
    public void active(Connection connection) {
//        String key =  connection.getProtocolEnum().getCode() + "#" + connection.getIp() + "#" + connection.getPort();
//        RedisUtil.Hash.put(PrefixConstants.IOT_CONDITIONS_IEC_PREFIX + myIp,key,true);
    }

    /**
     * 连接后，断连会被监听，然后调这个方法
     * @param connection
     */
    @Override
    public void inActive(Connection connection) {
//        String key =  connection.getProtocolEnum().getCode() + "#" + connection.getIp() + "#" + connection.getPort();
//        RedisUtil.Hash.put(PrefixConstants.IOT_CONDITIONS_IEC_PREFIX + myIp,key,false);
    }

}
